package com.itcat.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.SmsHomeAdvertiseApi;
import com.itcat.mapper.SmsHomeAdvertiseMapper;
import com.itcat.mysql.pojo.SmsHomeAdvertise;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeAdvertiseApiImpl implements SmsHomeAdvertiseApi {

    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    //分页查询广告
    @Override
    public Page<SmsHomeAdvertise> findList(String endTime, Integer pageNum, Integer pageSize, String name, Integer type) {
        Page<SmsHomeAdvertise> page = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<SmsHomeAdvertise> aqw = new LambdaQueryWrapper<>();
        aqw.eq(name!=null,SmsHomeAdvertise::getName,name)
                .eq(endTime!=null,SmsHomeAdvertise::getEndTime,endTime)
                .eq(type!=null,SmsHomeAdvertise::getType,type);
        Page<SmsHomeAdvertise> selectPage = smsHomeAdvertiseMapper.selectPage(page, aqw);


        return selectPage;
    }

    //查询广告详
    @Override
    public SmsHomeAdvertise queryOne(Integer id) {
        LambdaQueryWrapper<SmsHomeAdvertise> aqw = new LambdaQueryWrapper<>();
        aqw.eq(SmsHomeAdvertise::getId,id);
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseMapper.selectOne(aqw);
        return smsHomeAdvertise;
    }

    //添加广告
    @Override
    public void create(SmsHomeAdvertise smsAdvertise) {
        SmsHomeAdvertise advertise = new SmsHomeAdvertise();
        BeanUtils.copyProperties(smsAdvertise,advertise);
        smsHomeAdvertiseMapper.insert(smsAdvertise);
    }

    //删除广告
    @Override
    public void delete(List<Integer> ids) {
        smsHomeAdvertiseMapper.deleteBatchIds(ids);
    }


    @Override
    public void deleteById(Integer id) {
        LambdaQueryWrapper<SmsHomeAdvertise> aqw = new LambdaQueryWrapper<>();
        aqw.eq(SmsHomeAdvertise::getId,id);
        SmsHomeAdvertise advertise = smsHomeAdvertiseMapper.selectOne(aqw);
        /*if (advertise.getStatus()==1){
            //如果不能删除，抛出一个业务异常
            throw new BusinessException(ResultCode.FAILED);
        }*/
        smsHomeAdvertiseMapper.deleteById(id);
    }

    //根据ID修改状态
    @Override
    public void updateStatus(Integer id, Integer status) {
        LambdaUpdateWrapper<SmsHomeAdvertise> aqw = new LambdaUpdateWrapper<>();
        aqw.eq(SmsHomeAdvertise::getId,id).set(SmsHomeAdvertise::getStatus,status);

        SmsHomeAdvertise advertise = new SmsHomeAdvertise();
        advertise.setStatus(status);
        smsHomeAdvertiseMapper.update(advertise,aqw);
    }

    //根据ID修改广告
    @Override
    public void updateById(Integer id,SmsHomeAdvertise smsHomeAdvertise) {
        LambdaUpdateWrapper<SmsHomeAdvertise> aqw = new LambdaUpdateWrapper<>();
        aqw.eq(SmsHomeAdvertise::getId,id).setEntity(smsHomeAdvertise);

        smsHomeAdvertiseMapper.update(smsHomeAdvertise,aqw);
    }
}
